package com.frontalcode 
{
	import fl.managers.*;
	import flash.display.*;
	import flash.events.*;
	
	public class FrontalFocusManager extends FocusManager
	{
		public function FrontalFocusManager ( container : DisplayObjectContainer )
		{
			super ( container );
			
			// Annoyingly the FocusManager does not check if its stage member is
			// null or not so we have to jump through hoops including listening
			// for add and remove stage events to activate / deactivate 
			// ourselves before the stage disappears.
			//
			container.addEventListener ( Event.ADDED_TO_STAGE,		stageChangeHandler );
			container.addEventListener ( Event.REMOVED_FROM_STAGE,	stageChangeHandler );
		}
		
		internal function stageChangeHandler ( event : Event ) : void
		{
			if ( event.type == Event.ADDED_TO_STAGE     ) activate ( );
			if ( event.type == Event.REMOVED_FROM_STAGE ) deactivate ( );
		}
		
		override public function activate   ( ) : void { if ( form.stage != null ) super.activate ( ); }
		override public function deactivate ( ) : void { if ( form.stage != null ) super.deactivate ( ); }
	}
}

